/**
 * @file    inf_solenoid_valve.h
 * @author  yhy
 * @brief   电磁阀硬件接口层头文件，用于控制电磁阀的开关
 * @version 1.03
 * @date    2025-07-20
 *
 * @copyright Copyright (c) 2025
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-07-20     yhy          the first version V1.00
 * 2025-08-19     yhy          修改缓升缓降逻辑, 加入了单独控制单步步进时间 V1.01
 * 2025-08-21     yhy          新增电磁阀动态占空比，会随着电压的变化而变化 V1.02
 * 2025-09-15     yhy          修复了缓升缓降12V延时bug V1.03
 *
 */
#ifndef __INF_SOLENOID_VALVE_H__
#define __INF_SOLENOID_VALVE_H__

#include "common_config.h"

#include <stdbool.h>
#include <stdint.h>

#define SOLENOID_VALVE_STEP_CYCLE   5     /* 电磁阀handle周期调度时间 5ms */
#define SOLENOID_VALVE_BASE_VOLTAGE 12000 /* 电磁阀基准电压 12V */

/**
 * @brief 电磁阀通道枚举, 对应移位寄存器的out序号
 */
typedef enum
{
    SOLENOID_VALVE_CH0  = 0U,
    SOLENOID_VALVE_CH1  = 1U,
    SOLENOID_VALVE_CH2  = 2U,
    SOLENOID_VALVE_CH3  = 3U,
    SOLENOID_VALVE_CH4  = 4U,
    SOLENOID_VALVE_CH5  = 5U,
    SOLENOID_VALVE_CH6  = 6U,
    SOLENOID_VALVE_CH7  = 7U,
    SOLENOID_VALVE_CH8  = 8U,
    SOLENOID_VALVE_CH9  = 9U,
    SOLENOID_VALVE_CH10 = 10U,
    SOLENOID_VALVE_CH11 = 11U,
    SOLENOID_VALVE_CH12 = 12U,
    SOLENOID_VALVE_CH13 = 13U,
    SOLENOID_VALVE_CH14 = 14U,
    SOLENOID_VALVE_CH15 = 15U,
    SOLENOID_VALVE_CH16 = 16U,
    SOLENOID_VALVE_CH17 = 17U,
    SOLENOID_VALVE_CH18 = 18U,
    SOLENOID_VALVE_CH19 = 19U,
    SOLENOID_VALVE_CH20 = 20U,
    SOLENOID_VALVE_CH21 = 21U,
    SOLENOID_VALVE_CH22 = 22U,
    SOLENOID_VALVE_CH23 = 23U,
    SOLENOID_VALVE_CH24 = 24U,
    SOLENOID_VALVE_CH25 = 25U,
    SOLENOID_VALVE_CH26 = 26U,
    SOLENOID_VALVE_CH27 = 27U,
    SOLENOID_VALVE_CH28 = 28U,
    SOLENOID_VALVE_CH29 = 29U,
    SOLENOID_VALVE_CH30 = 30U,
    SOLENOID_VALVE_CH31 = 31U,
    SOLENOID_VALVE_CH_MAX,
} solenoid_valve_channel_e;

/**
 * @brief 电磁阀状态枚举
 *
 */
typedef enum
{
    SOLENOID_VALVE_STATE_DISABLED  = 0U, /*!< 失能状态  */
    SOLENOID_VALVE_STATE_RAMP_UP   = 1U, /*!< 缓升状态  */
    SOLENOID_VALVE_STATE_ENABLED   = 2U, /*!< 使能状态  */
    SOLENOID_VALVE_STATE_RAMP_DOWN = 3U, /*!< 缓降状态  */
} solenoid_valve_state_e;

/**
 * @brief 电压等级枚举（对应PWM占空比）
 *
 */
typedef enum
{
    VOLTAGE_LEVEL_0V   = 0U,
    VOLTAGE_LEVEL_2V   = 17U,
    VOLTAGE_LEVEL_4V   = 33U,
    VOLTAGE_LEVEL_6V   = 50U,
    VOLTAGE_LEVEL_6P5V = 54U, /*!< 6.5V */
    VOLTAGE_LEVEL_7V   = 59U,
    VOLTAGE_LEVEL_8V   = 67U,
    VOLTAGE_LEVEL_9V   = 75U,
    VOLTAGE_LEVEL_10V  = 83U,
    VOLTAGE_LEVEL_12V  = 100U,
} voltage_level_e;

/**
 * @brief 电压等级步进结构, 用于处理缓升缓降
 *
 */
typedef struct
{
    voltage_level_e voltage_level; /*!< 电压等级 */
    uint32_t        step_time_ms;  /*!< 步进时间, 当前电压等级的持续时间 */
} voltage_level_step_t;

/**
 * @brief 单个电磁阀控制参数, 一个结构体实例代表一个电磁阀
 *
 */
typedef struct
{
    solenoid_valve_state_e state;         /*!< 当前状态  */
    bool                   target_enable; /*!< 目标使能状态  */
    uint8_t                current_duty;  /*!< 当前占空比  */
    uint32_t               step_idx;      /*!< 步进计数器  */
    uint32_t               step_delay;    /*!< 步进延时  */
} solenoid_valve_t;

/**
 * @brief 电磁阀系统结构体, 用于控制整个PCB上的电磁阀
 *
 */
typedef struct
{
    solenoid_valve_t valve_list[SOLENOID_VALVE_CH_MAX]; /*!< 电磁阀参数列表, 32个电磁阀 */
    bool             system_enable;                     /*!< 系统总使能  */
} solenoid_valve_system_t;

extern solenoid_valve_system_t valve_system;
extern float                   voltage_comp_factor;

result_e inf_solenoid_valve_init(void);
result_e inf_solenoid_valve_set_system_enable(bool enable);
result_e inf_solenoid_valve_set_enable(solenoid_valve_channel_e channel, bool enable);
result_e inf_solenoid_valve_set_all_valves(bool enable);
void     inf_solenoid_valve_ctrl_handle(void);

#endif
